package com.spider.spiderorderadmin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spider.spiderorderadmin.common.Result;
import com.spider.spiderorderadmin.common.annotation.OperationAspect;
import com.spider.spiderorderadmin.entity.dto.ResourceTreeNode;
import com.spider.spiderorderadmin.entity.dto.SystemResourceDTO;
import com.spider.spiderorderadmin.entity.model.SystemResource;
import com.spider.spiderorderadmin.entity.param.SystemResourceQueryParam;
import com.spider.spiderorderadmin.service.SystemResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author xiong.bo
 * @version 1.0
 * @date 2020/11/29 13:11
 */

@Api(tags = "系统资源管理")
@RestController
@RequestMapping("/system/resource")
public class SystemResourceController extends AbstractController{

    @Resource
    private SystemResourceService resourceService;

    @ApiOperation("系统资源列表")
    @GetMapping
    @OperationAspect(accessPath = "/system/resource/selectPage", accessDesc = "系统资源列表")
    public Result<Page<SystemResource>> selectPage(SystemResourceQueryParam queryParam) {
        return Result.of(resourceService.selectPage(queryParam));
    }

    @ApiOperation("系统资源树形结构")
    @GetMapping("/menuTreeData")
    @OperationAspect(accessPath = "/system/resource/selectTreeResource", accessDesc = "系统资源树形结构")
    public Result<List<ResourceTreeNode>> selectTreeResource() {
        return Result.of(resourceService.selectTreeResource());
    }

    @ApiOperation("系统资源详情")
    @GetMapping("/{id}")
    @OperationAspect(accessPath = "/system/resource/findById", accessDesc = "系统资源详情")
    public Result<SystemResource> findById(@PathVariable(name = "id") Long id) {
        return Result.of(resourceService.findById(id));
    }

    @ApiOperation("新增系统资源")
    @PostMapping
    @OperationAspect(accessPath = "/system/resource/save", accessDesc = "新增系统资源")
    public Result<SystemResource> save(@Validated @RequestBody SystemResourceDTO systemResourceDTO) {
        return Result.of(resourceService.save(systemResourceDTO, super.getUserContentId()));
    }

    @ApiOperation("修改系统资源")
    @PutMapping
    @OperationAspect(accessPath = "/system/resource/update", accessDesc = "修改系统资源")
    public Result<SystemResource> update(@Validated @RequestBody SystemResourceDTO systemResourceDTO) {
        return Result.of(resourceService.update(systemResourceDTO, super.getUserContentId()));
    }

    @ApiOperation("批量删除系统资源")
    @DeleteMapping("/delete")
    @OperationAspect(accessPath = "/system/resource/delete",accessDesc = "批量删除系统资源")
    public Result<Boolean> delete(@RequestParam("ids") String ids) {
        return Result.of(resourceService.deleteByIds(ids));
    }

}
